﻿using COFE.Core;
using COFE.Core.Entry;
using COFE.Core.Lister;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace COFE.Data
{
    /// <summary>
    /// Currently mainly used to provide DatabaseProperties.LastListTime
    /// </summary>
    public class DBLastListTimePropertyProvider : ISecondaryPropertyProvider<IFileSystemInfo>
    {
        public PropertyPair GetProperty(IFileSystemInfo sourceObj, object property)
        {
            if (property.Equals(DatabaseProperties.LastListTime) && sourceObj is IDirectoryInfo)
            {
                var sourceDir = sourceObj as IInternalDirectoryInfoExA;
                if (sourceDir.PrimaryPropertyProvider is ProfileRootPropertyProvider)
                    return new PropertyPair(property, DateTime.UtcNow);

                var retVal = COFEDBShared.DBOperations.ReadDatabase(
                    (context) =>
                    {
                        var dbEntry = COFEDBShared.DBEntryManager.LookupEntry(context, sourceObj.GetId());
                        if (dbEntry != null)
                            return COFEDBShared.DBEntryPropertyManager.GetProperty(context, dbEntry, property);
                        else return COFEDBShared.DTMin;
                    });

                return new PropertyPair(property, retVal);
            }

            throw new NotSupportedException();
        }

        public object[] GetSupportedProperty(IFileSystemInfo sourceObj)
        {
            if (sourceObj is IDirectoryInfo)
                return new object[] { DatabaseProperties.LastListTime };
            return new object[] { };
        }

        public bool IsPropertyReadOnly(IFileSystemInfo sourceObj, object property)
        {
            return true;
        }

        public void SetProperty(IFileSystemInfo sourceObj, object property, object value)
        {
            throw new NotImplementedException();
        }
    }
}
